﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.Client
{
    public partial class SpeedwayConfig : XtraUserControl
    {
        public SpeedwayConfig()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                #region 检查

                string strInStockIp = txtInStockIp.Text.Trim();
                if (string.IsNullOrEmpty(strInStockIp) ||
                    (chkInStockAntenna1.Checked || chkInStockAntenna2.Checked || chkInStockAntenna3.Checked ||
                    chkInStockAntenna4.Checked || chkInStockAntenna5.Checked || chkInStockAntenna6.Checked) == false)
                {
                    AlertForm.ShowMessage("入库链接不合法");
                    return;
                }

                string strOutStockIp = txtOutStockIp.Text.Trim();
                if (string.IsNullOrEmpty(strOutStockIp) ||
                    (chkOutStockAntenna1.Checked || chkOutStockAntenna2.Checked || chkOutStockAntenna3.Checked ||
                    chkOutStockAntenna4.Checked || chkOutStockAntenna5.Checked || chkOutStockAntenna6.Checked) == false)
                {
                    AlertForm.ShowMessage("出库链接不合法");
                    return;
                }

                string strStockCheckIp = txtStockCheckIp.Text.Trim();
                if (string.IsNullOrEmpty(strStockCheckIp) ||
                    (chkStockCheckAntenna1.Checked || chkStockCheckAntenna2.Checked || chkStockCheckAntenna3.Checked ||
                    chkStockCheckAntenna4.Checked || chkStockCheckAntenna5.Checked || chkStockCheckAntenna6.Checked) == false)
                {
                    AlertForm.ShowMessage("盘点链接不合法");
                    return;
                }

                string strOutStockCheckIp = txtOutStockCheckIp.Text.Trim();
                if (string.IsNullOrEmpty(strOutStockCheckIp) ||
                    (chkOutStockCheckAntenna1.Checked || chkOutStockCheckAntenna2.Checked || chkOutStockCheckAntenna3.Checked ||
                    chkOutStockCheckAntenna4.Checked || chkOutStockCheckAntenna5.Checked || chkOutStockCheckAntenna6.Checked) == false)
                {
                    AlertForm.ShowMessage("出库复核链接不合法");
                    return;
                }

                #endregion

                #region 保存

                string data = string.Empty;
                data += chkInStockAntenna1.Checked ? "1," : string.Empty;
                data += chkInStockAntenna2.Checked ? "2," : string.Empty;
                data += chkInStockAntenna3.Checked ? "3," : string.Empty;
                data += chkInStockAntenna4.Checked ? "4," : string.Empty;
                data += chkInStockAntenna5.Checked ? "5," : string.Empty;
                data += chkInStockAntenna6.Checked ? "6," : string.Empty;
                data = string.Format("{0}|{1}", strInStockIp, data.TrimEnd(','));
                SpeedwayPublicFunction.WriteInStockSpeedwayCfg(data);

                data = string.Empty;
                data += chkOutStockAntenna1.Checked ? "1," : string.Empty;
                data += chkOutStockAntenna2.Checked ? "2," : string.Empty;
                data += chkOutStockAntenna3.Checked ? "3," : string.Empty;
                data += chkOutStockAntenna4.Checked ? "4," : string.Empty;
                data += chkOutStockAntenna5.Checked ? "5," : string.Empty;
                data += chkOutStockAntenna6.Checked ? "6," : string.Empty;
                data = string.Format("{0}|{1}", strOutStockIp, data.TrimEnd(','));
                SpeedwayPublicFunction.WriteOutStockSpeedwayCfg(data);

                data = string.Empty;
                data += chkStockCheckAntenna1.Checked ? "1," : string.Empty;
                data += chkStockCheckAntenna2.Checked ? "2," : string.Empty;
                data += chkStockCheckAntenna3.Checked ? "3," : string.Empty;
                data += chkStockCheckAntenna4.Checked ? "4," : string.Empty;
                data += chkStockCheckAntenna5.Checked ? "5," : string.Empty;
                data += chkStockCheckAntenna6.Checked ? "6," : string.Empty;
                data = string.Format("{0}|{1}", strStockCheckIp, data.TrimEnd(','));
                SpeedwayPublicFunction.WriteStockCheckSpeedwayCfg(data);

                data = string.Empty;
                data += chkOutStockCheckAntenna1.Checked ? "1," : string.Empty;
                data += chkOutStockCheckAntenna2.Checked ? "2," : string.Empty;
                data += chkOutStockCheckAntenna3.Checked ? "3," : string.Empty;
                data += chkOutStockCheckAntenna4.Checked ? "4," : string.Empty;
                data += chkOutStockCheckAntenna5.Checked ? "5," : string.Empty;
                data += chkOutStockCheckAntenna6.Checked ? "6," : string.Empty;
                data = string.Format("{0}|{1}", strOutStockCheckIp, data.TrimEnd(','));
                SpeedwayPublicFunction.WriteOutCheckSpeedwayCfg(data);

                AlertForm.ShowMessage("保存成功");
                #endregion
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void SpeedwayConfig_Load(object sender, EventArgs e)
        {
            try
            {
                string[] readerCfg = SpeedwayPublicFunction.GetInStockSpeedwayCfg();
                if (readerCfg != null)
                {
                    txtInStockIp.Text = readerCfg[0];
                    List<string> lst = new List<string>();
                    string[] numberList = readerCfg[1].Split(',');
                    lst.AddRange(numberList);
                    chkInStockAntenna1.Checked = lst.Contains("1") ? true : false;
                    chkInStockAntenna2.Checked = lst.Contains("2") ? true : false;
                    chkInStockAntenna3.Checked = lst.Contains("3") ? true : false;
                    chkInStockAntenna4.Checked = lst.Contains("4") ? true : false;
                    chkInStockAntenna5.Checked = lst.Contains("5") ? true : false;
                    chkInStockAntenna6.Checked = lst.Contains("6") ? true : false;
                }
                readerCfg = SpeedwayPublicFunction.GetOutStockSpeedwayCfg();
                if (readerCfg != null)
                {
                    txtOutStockIp.Text = readerCfg[0];
                    List<string> lst = new List<string>();
                    string[] numberList = readerCfg[1].Split(',');
                    lst.AddRange(numberList);
                    chkOutStockAntenna1.Checked = lst.Contains("1") ? true : false;
                    chkOutStockAntenna2.Checked = lst.Contains("2") ? true : false;
                    chkOutStockAntenna3.Checked = lst.Contains("3") ? true : false;
                    chkOutStockAntenna4.Checked = lst.Contains("4") ? true : false;
                    chkOutStockAntenna5.Checked = lst.Contains("5") ? true : false;
                    chkOutStockAntenna6.Checked = lst.Contains("6") ? true : false;
                }
                readerCfg = SpeedwayPublicFunction.GetStockCheckSpeedwayCfg();
                if (readerCfg != null)
                {
                    txtStockCheckIp.Text = readerCfg[0];
                    List<string> lst = new List<string>();
                    string[] numberList = readerCfg[1].Split(',');
                    lst.AddRange(numberList);
                    chkStockCheckAntenna1.Checked = lst.Contains("1") ? true : false;
                    chkStockCheckAntenna2.Checked = lst.Contains("2") ? true : false;
                    chkStockCheckAntenna3.Checked = lst.Contains("3") ? true : false;
                    chkStockCheckAntenna4.Checked = lst.Contains("4") ? true : false;
                    chkStockCheckAntenna5.Checked = lst.Contains("5") ? true : false;
                    chkStockCheckAntenna6.Checked = lst.Contains("6") ? true : false;
                }
                readerCfg = SpeedwayPublicFunction.GetOutCheckSpeedwayCfg();
                if (readerCfg != null)
                {
                    txtOutStockCheckIp.Text = readerCfg[0];
                    List<string> lst = new List<string>();
                    string[] numberList = readerCfg[1].Split(',');
                    lst.AddRange(numberList);
                    chkOutStockCheckAntenna1.Checked = lst.Contains("1") ? true : false;
                    chkOutStockCheckAntenna2.Checked = lst.Contains("2") ? true : false;
                    chkOutStockCheckAntenna3.Checked = lst.Contains("3") ? true : false;
                    chkOutStockCheckAntenna4.Checked = lst.Contains("4") ? true : false;
                    chkOutStockCheckAntenna5.Checked = lst.Contains("5") ? true : false;
                    chkOutStockCheckAntenna6.Checked = lst.Contains("6") ? true : false;
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }


    }
}
